package com.net.xpay.common.manager.impl;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.ApiMonitorMapper;
import com.net.xpay.common.domain.ApiMonitor;
import com.net.xpay.common.manager.ApiMonitorManager;
import com.net.xpay.common.model.ApiMonitorAggModel;
import com.net.xpay.core.model.ApiMonitorSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Component
public class ApiMonitorManagerImpl extends AbstractManager<ApiMonitor> implements ApiMonitorManager{

    @Autowired
    private ApiMonitorMapper mapper;


    @Override
    protected BasicMapper<Long, ApiMonitor> getMapper() {
        return mapper;
    }

    @Override
    public List<ApiMonitorAggModel> aggSearch(ApiMonitorSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        List<ApiMonitorAggModel> aggApiMonitorModels = mapper.aggSearch(searchModel, page);
        return aggApiMonitorModels  == null ? new ArrayList<>() : aggApiMonitorModels;
    }

    @Override
    public long aggSearchCount(ApiMonitorSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.aggSearchCount(searchModel);
    }

    @Override
    public ApiMonitor getByApi(String api) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(api));
        ApiMonitor condition = new ApiMonitor();
        condition.setApi(api);
        return getByCondition(condition);
    }

    @Override
    public int deleteByMaxDay(LocalDate day) {
        Preconditions.checkArgument(day != null);
        return mapper.deleteByMaxDay(day);
    }
}
